<?php

namespace app\models;

use yii;
use yii\db\Query;

/**
 *
 *内陆费用表
 */
class Inland extends BaseModel
{
    public function __construct()
    {
        $this->table = yii::$app->db->tablePrefix . 'inland_fee';
    }

    public function all($where = [], $offset = false, $limit = false, $order = false)
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['invoice'])) {
            if (is_array($where['invoice'])) {
                $db->andWhere(['in', 'invoice', $where['invoice']]);
            } else {
                $db->andWhere(['invoice' => $where['invoice']]);
            }
        }
        if (!empty($where['pid'])) $db->andWhere(['pid' => $where['pid']]);
        return $db->all();
    }

    public function one($where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if (!empty($where['id'])) {
            $db->andWhere(['id' => $where['id']]);
        }
        if (!empty($where['pid'])) {
            $db->andWhere(['pid' => $where['pid']]);
        }
        return $db->limit(1)->one();
    }

    public function get_inland_list($p, $s, $where = [])
    {
        $db = new Query();
        $db->select('*')->from($this->table);
        if ($where['saleman'])
            $db->andWhere(['saleman' => $where['saleman']]);
        if ($where['invoice'])
            $db->andWhere(['invoice' => $where['invoice']]);
        if ($where['customer_num'])
            $db->andWhere(['customer_num' => $where['customer_num']]);
        if ($where['customer_name'])
            $db->andWhere(['like', 'customer_name', $where['customer_name']]);
        if ($where['order_num'])
            $db->andWhere(['like', 'order_num', trim($where['order_num'])]);
        if ($where['proxy_company'])
            $db->andWhere(['proxy_company' => $where['proxy_company']]);

        $list = $db->orderBy(['date' => SORT_DESC, 'pid' => SORT_DESC, 'id' => SORT_ASC])->offset(($p - 1) * $s)->limit($s)
            ->all();
        return $list;
    }

    public function get_inland_size($where = [])
    {
        $db = new Query();
        $db->select('count(id) as num')->from($this->table);
        if ($where['saleman']) {
            $db->andWhere(['saleman' => $where['saleman']]);
        }

        if ($where['invoice']) {
            $db->andWhere(['invoice' => $where['invoice']]);
        }

        if ($where['customer_num']) {
            $db->andWhere(['customer_num' => $where['customer_num']]);
        }

        if ($where['customer_name']) {
            $db->andWhere(['like', 'customer_name', $where['customer_name']]);
        }
        if ($where['order_num']) {
            $db->andWhere(['like', 'order_num', trim($where['order_num'])]);
        }
        if ($where['proxy_company']) {
            $db->andWhere(['proxy_company' => $where['proxy_company']]);
        }

        $info = $db->one();

        return $info ? $info['num'] : 0;
    }

    public function get_inland_list_by_invoice($invoice)
    {
        $db = new Query();
        $list = $db->select('invoice,sum(total_fee) as total_fee,date,remark,customer_num')
            ->from($this->table)
            ->where(['in', 'invoice', $invoice])
            ->groupBy(['invoice'])
            ->all();
        return $list;
    }

    public function getListByPid($arr)
    {
        $db = new Query();
        return $db->select('*')->from($this->table)->where(['in', 'pid', $arr])->all();
    }

}
